﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     ContactList业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/25
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class ContactListBLL
    {
        private static ContactListBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IContactListDao dao = null;

        private ContactListBLL()
        {
            dao = ContactListSqlMapDao.Instance;
        }

        public static ContactListBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new ContactListBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<ContactList> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<ContactList> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<ContactList> GetListByParameters(object TopCounts, ContactList contactlist)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<ContactList> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="ContactID"></param>
        /// <returns></returns>
        public ContactList GetDetail(System.String contactid)
        {
            try
            {
                ///daoManager.BeginTransaction();
                ContactList contact = new ContactList();
                contact.ContactID = contactid;
                return dao.GetDetail(contact);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="ContactID"></param>
        /// <returns></returns>
        public ContactList GetDetail(ContactList contact)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(contact);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 添加联系人
        /// 每发送一条信息，则同时为发信人及收件人添加或更新联系人信息
        /// </summary>
        /// <param name="contactlist"></param>
        /// <returns>返回添加后的ContactID</returns>
        public void Add(ContactList contact)
        {
            try
            {
                ContactList ret = dao.GetDetail(contact);
                //联系人已存在则更新该条数据，并将IsDel设为未删除
                if (ret != null)
                {
                    ret.LatestMessage = contact.LatestMessage;
                    ret.UpdateDate = contact.UpdateDate;
                    ret.State = (int)Common.Enum.auvState.Enabled;
                    ret.IsDel = (int)Common.Enum.auvDel.NotDel;
                    Update(ret);
                }
                else
                {
                    contact.ContactID = Guid.NewGuid().ToString();
                    //第一条消息记录
                    contact.FirstMessage = contact.LatestMessage;
                    contact.State = (int)Common.Enum.auvState.Enabled;
                    contact.IsDel = (int)Common.Enum.auvDel.NotDel;
                    contact.CreateDate = DateTime.Now;
                    contact.BeginDate = DateTime.Now;
                    contact.EndDate = DateTime.Now;
                    dao.Add(contact);
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="contactlist"></param>
        /// <returns></returns>
        public int Update(ContactList contactlist)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(contactlist);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ContactID"></param>
        /// <returns></returns>
        public int Delete(System.String contactid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(contactid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="contactlist"></param>
        /// <returns></returns>
        public int Delete(ContactList contactlist)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(contactlist);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 获取联系人列表的分页数据
        /// </summary>
        /// <param name="pager"></param>
        /// <returns></returns>
        public IList<ContactList> GetPagedList(string userid, string type, int pagesize, int pageindex, ref int recordcount)
        {
            try
            {
                string sqlwhere = string.Format("UserID='{0}' and State={1} and IsDel={2}", userid, (int)Common.Enum.auvState.Enabled, (int)Common.Enum.auvDel.NotDel);

                //显示方式   
                //我发起的
                if (type == "fromme")
                {
                    sqlwhere += " and Sender='" + userid + "'";
                }
                //对方发起的
                else if (type == "tome")
                {
                    sqlwhere += " and Sender!='" + userid + "'";
                }
                Model.NetPager pager = new Model.NetPager();
                pager.TableName = "(select b.Sender,a.* from ContactList a left join Message b on a.FirstMessage=b.MessageID) as TEMP";
                pager.PrimaryKey = "ContactID";
                pager.PageIndex = pageindex;
                pager.PageSize = pagesize;
                pager.Order = "UpdateDate Desc,ContactID asc";
                pager.SortType = (int)Common.Enum.SortType.Multi;
                pager.Where = sqlwhere;
                IList<Model.ContactList> list = dao.GetPagedList(pager);

                recordcount = pager.TotalCount;

                return list;
            }
            catch
            {

                throw;
            }

        }

        //删除与某用户的全部对话
        public void DeleteDialog(ContactList contact)
        {
            try
            {
                //隐藏联系人列表
                Update(contact);

                //修改信息的状态

                //获取该对话下所有信息,并修改状态
                //我发给别人的
                IList<Message> sendlist = MessageBLL.Instance.GetMessageBySenderAndReceiver(contact.UserID, contact.ContactUserID, contact.UserID);
                if (sendlist.Count > 0)
                {
                    MessageBLL.Instance.DeleteSendList(sendlist);
                }

                //别人发给我的
                IList<Message> receivelist = MessageBLL.Instance.GetMessageBySenderAndReceiver(contact.ContactUserID, contact.UserID, contact.UserID);
                if (receivelist.Count > 0)
                {
                    MessageBLL.Instance.DeleteReceiveList(receivelist);
                }
            }
            catch
            {

                throw;
            }
        }


    }
}
